/**
*	插件：选择部门用户
*	开发者：雨中磐石
*	地址：www.rockoa.com
*	日期：2023-07-17
*/

js.changeuser = {
	create:function(cans){
		var me = this;
		var defv = js.applyIf(cans,{
			rand:js.getrand(),
			title:'',
			cnum:'',
			changetype:'',
			changesel:'',
			checked:false,
			width:420,
			onselect:function(){}
		});
		for(var i in defv)this[i]=defv[i];
		var rand  = this.rand;
		
		this._init	= function(){
			this.ismobile 	= js.web.ismobile();
			this.userarr 	= [];
			this.deptarr 	= [];
			this.grouparr	= [];
			this._showcreate();
		};
		
		this._showcreate = function(){
			var type='checkbox';
			this.checked 	= (this.changetype.indexOf('check')>-1);
			if(!this.checked)type='radio';
			this.inputtype 	= type;
			var s3 = '';
			if(this.changetype.indexOf('user')>=0)s3+='<option value="1">仅限显示人员</option>';
			if(this.changetype.indexOf('dept')>=0)s3+='<option value="2">仅限显示部门</option>';
			//if(this.changetype.indexOf('user')>=0)s3+='<option value="3">显示组</option>';
			
			this.dob = this.changetype.indexOf('dept')>=0;
			this.uob = this.changetype.indexOf('user')>=0;
			
			var hei  = winHb()-200;
			if(hei>480)hei = 480;
			var s='	<div style="height:40px;overflow:hidden;border-bottom:1px #cccccc solid;padding:0px 0px"><table width="100%" style="background:white"><tr><td><select id="selxuan_'+this.rand+'" style="width:120px;border:none;background:none;display:"><option value="">默认显示</option>'+s3+'</select></td><td width="100%" height="40"><input id="changekey_'+this.rand+'" placeholder="部门/姓名/职位" style="height:30px;border:none;background:none;width:100%;margin:0px 10px;outline:none"></td><td nworap><button class="cursor" style="background:none;color:#666666;border:none;width:50px" id="changesoubtn_'+this.rand+'" type="button" >查找</button></td></tr></table></div>';
			this.mainid = 'selectlist_'+rand+'';
			s+='<div style="-webkit-overflow-scrolling:touch;height:'+hei+'px;overflow:auto;" class="rock-panel">';
			s+='<div id="'+this.mainid+'"></div>';
			s+='<div id="'+this.mainid+'sou" style="display:none"></div>';
			s+='</div>';
			
			var act = 'select'+rand+'';
			this.view = act;
			js.modal.main({
				title:this.title,
				action:act,
				width:this.width,
				height:500,
				btn:[{text:'<i class="icon-refresh"></i>',bgcolor:';background:none;border:none;',cls:'webbtn-default'},{text:'确定'}],
				html:s
			});
			this.loaddata();
			$('#'+act+'_btn1').click(function(){
				me.queding();
			});
			$('#'+act+'_btn0').click(function(){
				me.loaddata();
			});
			$('#selxuan_'+rand+'').change(function(){
				me._changesel(this);
			});
			$('#changekey_'+this.rand+'').keydown(function(e){
				me._searchkeys(e)
			});
		}
		
		this.loaddata = function(){
			$('#selectlist_'+rand+'').html('<div align="center" style="padding-top:50px" ><i style="height:40px;width:40px" class="rock-loading"></i></div>');
			js.ajax.get({
				url:'api/company/userdata',
				cnum:this.cnum,
				data:{changetype:this.changetype},
				onsuccess:function(ret){
					me.showdata(ret.data, true);
				},
				onerror:function(ret){
					$('#'+me.mainid+'').html(ret.msg);
				}
			});
		}
		
		this._changesel=function(o1){
			var val = o1.value;
			this.changesel = val;
			this.showlist();
		};
		
		this.hide = function(){
			js.modal.close(this.view);
		}
		
		this.showdata=function(da,inb){
			this.data 	  = da;
			this.deptarr  = this.chuliddata(da.deptarr);
			this.userarr  = this.chuliudata(da.userarr);
			this.grouparr = da.grouparr;
			
			this.showlist();
		}
		
		this.showlist=function(){
			var sel = this.changesel;
			if(this.treet)this.treet.remove();
			this.treet = false;
			
			if(sel=='1'){
				this.nowdata = this.userarr;
				this.listshow(this.userarr);
			}else if(sel=='2'){
				this.nowdata = this.deptarr;
				this.listshow(this.deptarr);
			}else if(sel=='3'){
				
			}else{
				var da = this.deptarr;
				if(this.uob)da = da.concat(this.userarr);
				importjs('tree','create', {
					data:da,
					view:this.mainid,
					rand:rand,
					firstid:'d0',
					openchliren:true,
					showstotal:false,
					active:false,
					nowobj:this,
					nowcls:'treet'
				});
			}
			
		};
		
		this.queding = function(){
			var sid='',sna='',i,xzarr=[],da=false;
			if(!this.issearch){
				if(this.treet){
					da = (this.checked) ? this.treet.getchecked() : this.treet.getcheckedone();
				}else{
					da = this.getchecked();
				}
			}else{
				da = this.getcheckedsou();
			}
			if(da)for(i=0;i<da.length;i++){
				sid+=','+da[i].id+'';
				sna+=','+da[i].name+'';
			}
			if(sid){
				sid = sid.substr(1);
				sna = sna.substr(1);
			}
			if(this.checked && this.dob && this.uob){
			}else{
				sid = sid.replace(/[a-z]/gi, '');
			}
			if(this.nameobj)this.nameobj.value = sna;
			if(this.idobj)this.idobj.value = sid;
			
			this.hide();
			this.onselect(sna, sid, da);
		}
		
		this.chuliddata = function(da,dob){
			var i,len = da.length,ds=[],d,val;
			if(this.idobj)val = this.idobj.value;
			for(i=0;i<len;i++){
				d = da[i];
				ds.push({
					id:'d'+d.id+'',
					name:d.name,
					pid:'d'+d.pid+'',
					checked:this.ischeckedd(val, d.id),
					inputtype: (this.dob) ? this.inputtype:''
				});
			}
			return ds;
		}
		
		this.chuliudata = function(da){
			var i,len = da.length,ds=[],d,val;
			if(this.idobj)val = this.idobj.value;
			
			for(i=0;i<len;i++){
				d = da[i];
				if(!d.face)d.face = 'images/noface.png';
				ds.push({
					id:'a'+d.id+'',
					name:d.name,
					stotal:0,
					icons:d.face,
					deptname:d.deptname,
					checked:this.ischeckedu(val, d.id),
					inputtype:this.inputtype,
					subname:d.ranking,
					pid:'d'+d.deptid+''
				});
			}
			return ds;
		}
		
		this.ischeckedu = function(val,id){
			var bo = false;
			if(!val || !this.uob)return bo;
			var vals = ','+val+',';
			if(vals.indexOf(','+id+',')>-1 || vals.indexOf(',a'+id+',')>-1)bo=true;
			return bo;
		}
		
		this.ischeckedd = function(val,id){
			var bo = false;
			if(!val || !this.dob)return bo;
			var vals = ','+val+',';
			if(vals.indexOf(','+id+',')>-1 || vals.indexOf(',d'+id+',')>-1)bo=true;
			return bo;
		}
		
		//显示数据
		this.listshow = function(da, vs){
			var i,len=da.length,d,s='';
			for(i=0;i<len;i++){
				s+= this.showitems(da[i],i);
			}
			if(vs){
				vs.html(s);
			}else{
				$('#'+this.mainid+'').html(s);
			}
		}
		
		this.showitems = function(d,i){
			var s = '',s1;
			s='<div class="rock-list-item rock-hover" onclick="">';
			if(d.icons)s+='<img src="'+d.icons+'" height="20px" align="absmiddle" class="radius5" width="20px"> ';
			s+=''+d.name+'';
			if(d.subname)s+='<span class="hui fontsize14">('+d.subname+')</span>';
			if(d.inputtype){
				s1='';
				if(d.checked)s1=' checked';
				if(d.disabled)s1=' disabled';
				s+='<font><input xu="'+i+'" onclick="" name="inputsel'+rand+'" style="width:18px;height:18px;" type="'+d.inputtype+'" value="'+d.id+'" '+s1+'></font>';
			}
			s+='</div>';
			return s;
		}
		

		this.getchecked = function(){
			return this.getcheckedval(this.nowdata, this.mainid);
		}
		this.getcheckedsou = function(){
			return this.getcheckedval(this.searchdata, this.mainid+'sou');
		}
		this.getcheckedval = function(da, sid){
			var ds = [],i,len,o,xu;
			var obj = $('#'+sid+'').find('input[name=inputsel'+rand+']');
			len = obj.length;
			for(i=0;i<len;i++){
				o = obj[i];
				if(!o.disabled && o.checked){
					xu = parseFloat($(o).attr('xu'));
					ds.push(da[xu]);
				}
			}
			return ds;
		}
		
		this._searchkeys=function(e){
			clearTimeout(this._searchkeystime);
			this._searchkeystime=setTimeout(function(){
				me._searchkey(false);
			},500);
		};
		this._searchkey = function(bo){
			var key = $('#changekey_'+this.rand+'').val(),d,ds=[],len,i;
			var o1  = $('#'+this.mainid+'sou'),o2= $('#'+this.mainid+'');
			this.issearch = false;
			if(key){
				var da  = this.userarr;
				if(!this.uob)da = this.deptarr;
				len 	= da.length;
				for(i=0;i<len;i++){
					d = da[i];
					if(
						(d.name && d.name.indexOf(key)>-1) || 
						(d.subname && d.subname.indexOf(key)>-1) || 
						(d.deptname && d.deptname.indexOf(key)>-1)
					){
						ds.push(d);
					}
				}
				o1.show();o2.hide();
				this.issearch 	= true;
				this.searchdata = ds;
				if(ds.length==0){
					o1.html('<div align="center" class="hui" style="margin-top:30px;">无('+key+')相关记录</div>');
				}else{
					this.listshow(ds,o1);
				}
			}else{
				o2.show();o1.hide();
			}
		};
		
		this._init();
	},
	open:function(na,fid,lx){
		new this.create({
			title:na,
			type:lx
		});
	}
}